//
//  BuyFlowWebViewController.m
//  HerconWaterFilter
//
//  Created by Kendama on 2017/5/12.
//  Copyright © 2017年 JianWei Chen. All rights reserved.
//

#import "WebViewController.h"
#import "IMYWebView.h"
#import "NavigatObject.h"

@interface WebViewController ()<UIWebViewDelegate,IMYWebViewDelegate>

@property (strong, nonatomic) IMYWebView *webview;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;

@end

@implementation WebViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self setNavigationBarTitle:self.title];
//    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"3E76F6"];
    
    [self setWebData];
    


//    [self setNavigationTitle:@"意见反馈" titleColor:[UIColor whiteColor]];
//    [self setRightButtonTitle:@"提交"];
//    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F9"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)setWebData
{
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":@"Mozilla/5.0 AppleWebKit/537.36 Dtston/17.1.1"}];
    
    [_webview removeFromSuperview];
    _webview = [[IMYWebView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    [self.view addSubview:_webview];
    _webview.delegate=self;
    NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
    [_webview loadRequest:request];
    
//    NSString *title = [NSString string];
//    NSString *pathName = [NSString string];

//    NSString* path = [[NSBundle mainBundle] pathForResource:@"aboutyuyu" ofType:@"html"];
//    NSURL* url = [NSURL fileURLWithPath:path];
//    NSURLRequest* requestURL = [NSURLRequest requestWithURL:url] ;
//    [_webview loadRequest:requestURL];
    switch (self.webViewType) {
        case Store:
        {
            NSURLRequest* requestURL = [NSURLRequest requestWithURL:self.url] ;
            [_webview loadRequest:requestURL];
        }
            break;
        case AboutWe:
        {
            NSString* path = [[NSBundle mainBundle] pathForResource:@"aboutyuyu" ofType:@"html"];
            NSURL* url = [NSURL fileURLWithPath:path];
            NSURLRequest* requestURL = [NSURLRequest requestWithURL:url] ;
            [_webview loadRequest:requestURL];
        }
            break;
        case UserAgreement:
        {
            NSString* path = [[NSBundle mainBundle] pathForResource:@"yuyuyonghuxieyi" ofType:@"html"];
            NSURL* url = [NSURL fileURLWithPath:path];
            NSURLRequest* requestURL = [NSURLRequest requestWithURL:url] ;
            [_webview loadRequest:requestURL];
        }
            break;
            
        default:
            break;
    }
    
    [self setNavigationBarTitle:self.title];
    //    [self.webview setHidden:YES];
    [self.view bringSubviewToFront:self.backBtn];
}


- (NSString*)getPreferredLanguage
{
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    
    NSArray * allLanguages = [defaults objectForKey:@"AppleLanguages"];
    
    NSString * preferredLang = [allLanguages objectAtIndex:0];
    
    NSLog(@"当前语言:%@", preferredLang);
    if (EXRACT_OBJECT(@"lan"))
    {
        if ([EXRACT_OBJECT(@"lan") isEqualToString:@"en"])
        {
            return @"-english";
        }
        else
        {
            return @"";
        }
    }
    else
    {
        if ([preferredLang isEqualToString:@"zh-Hans"] || [preferredLang isEqualToString:@"zh-Hans-CN"] || [preferredLang isEqualToString:@"zh-Hant"] || [preferredLang isEqualToString:@"zh-Hant-CN"] || [preferredLang isEqualToString:@"zh-Hant-HK"] || [preferredLang isEqualToString:@"zh-Hant-TW"] || [preferredLang isEqualToString:@"zh-Hant-MO"])
        {
            return @"";
        }
        else
        {
            return @"-english";
        }
    }

    
}

//- (void)onLeftButtonClick:(id)sender
//{
//    [[AppDelegate defaultService].menuViewController showLeftViewController];
//}

- (void)onRightButtonClick:(id)sender
{
    
}

- (IBAction)backAction:(UIButton *)sender
{
//    [self dismissViewControllerAnimated:YES completion:nil];
}




-(void)webView:(IMYWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"didFailLoadWithError=====%@",error.description);
    
}
-(void)webViewDidFinishLoad:(IMYWebView *)webView
{
    
    NSLog(@"webViewDidFinishLoad=====%@",webView);
}
-(BOOL)webView:(IMYWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"===========shouldStartLoadWithRequest========%@====%ld",request.URL.absoluteString,(long)navigationType);
    
    NavigatObject *naviga=[[NavigatObject alloc]init];
    [naviga stringToHtmlString:request.URL.absoluteString];
    
    if ([naviga.Paramsdic[@"action"] isEqualToString:@"OPEN_WEBVIEW"]) {
        
        [self OPEN_WEBVIEW:naviga.Paramsdic];
        
        return NO;
    }else if ([request.URL.absoluteString rangeOfString:@"UPDATE_ADVICE"].location != NSNotFound){
        [self UPDATE_ADVICE:naviga.Paramsdic];
    }
    
    return YES;
}

- (void)OPEN_WEBVIEW:(NSDictionary *)paramsDic
{
    NSArray *nameArray = [paramsDic[@"url"] componentsSeparatedByString:@"."];
    WebViewController *vc = [[WebViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:nameArray[0] ofType:@".html"]];
    NSString *str = [url.absoluteString componentsSeparatedByString:[NSString stringWithFormat:@"%@%@",nameArray[0],@".html"]][0];
    url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",str,paramsDic[@"url"]]];
    vc.url = url;
    vc.title = paramsDic[@"title"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)UPDATE_ADVICE:(NSDictionary *)paramsDic
{
    [self getAdList:paramsDic[@"phone"] andConten:paramsDic[@"content"]];
}

- (void)getAdList:(NSString *)phone andConten:(NSString *)content
{
    //    UserFeedback * getApi = [[UserFeedback alloc] initWithPhone:phone andConten:content];
    //
    //    [getApi startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
    //        if ([getApi analyseResponseObject]) {
    //            [self.navigationController popViewControllerAnimated:YES];
    //        }
    //    } failure:^(YTKBaseRequest *request) {
    //        [SVProgressHUD showInfoWithStatus:@"网络异常"];
    //    }];
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    NSLog(@"main dealloc  `");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
